﻿using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Tssipl.PaymentAdapter.Core;
namespace Tssipl.PaymentAdapter.MessageFormat.Swift
{
    public class SwiftBlock3 : IBlock
    {
        //HACK private List<Field> _fields = new ArrayList<Field>();
        private List<Field> _fields = new List<Field>();

        /** Package-private constructor that extracts block 3
         * contents from the contents of the block (everything between
         * the {3: and the close } non-inclusive)
         * 
         * @param Contents the block's contents
         */
        public SwiftBlock3(String Contents)
        {

            String[] sections = Contents.Split("\\{".ToCharArray());//HACK ToCharArray
            if (sections != null && sections.Length > 0)
            {
                //HACK for (String part : sections)
                foreach (String part in sections)
                {
                    String segment = part.Trim();
                    if (segment.Length == 0)
                        continue;

                    if (!segment.EndsWith("}"))
                        continue;

                    segment = segment.SubstringJava(0, segment.Length - 1).Trim();

                    int index = segment.IndexOf(':');
                    if (index >= 0)
                    {
                        if (index == 0)
                            _fields.Add(new Field("", segment.Substring(1).Trim()));
                        else if (index == segment.Length)
                            _fields.Add(new Field(segment.SubstringJava(0, segment.Length - 1), ""));
                        else
                            _fields.Add(new Field(segment.SubstringJava(0, index).Trim(), segment.Substring(index + 1).Trim()));
                    }
                    else
                        _fields.Add(new Field(segment, ""));
                }
            }
        }


        /** Returns a properly formatted block 3 including braces and 3: identifier.
         * 
         * @return a complete block 3.
         */
        //@Override
        public override String ToString()
        {
            return getFormattedBlock();
        }



        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getContents()
         */
        public String getContents()
        {
            StringBuilder sb = new StringBuilder(512);
            //for (Field field : _fields)
            foreach (Field field in _fields)
            {
                sb.Append("{");
                sb.Append(field.getTag());
                sb.Append(":");
                sb.Append(field.getValue());
                sb.Append("}");
            }
            return sb.ToString();
        }


        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getFormattedBlock()
         */
        public String getFormattedBlock()
        {
            StringBuilder sb = new StringBuilder(512);
            sb.Append("{3:");
            sb.Append(getContents());
            sb.Append("}");
            return sb.ToString();
        }
    }
}